<template>
  <div ref="layerProjectModal" v-if="visible">
    <a-modal width="760px" :getContainer="() => $refs.layerProjectModal" :visible="visible" :title="TITLE[showType]" @cancel="handleCancel">
      <DynamicForm :config="dyConfig" :questions="dyQuestions" ref="directoryRef">
        <template #memberIds="{ question }">
          <a-select allow-clear :options="localState.memberIdsOptions" placeholder="请选择" mode="multiple" v-model:value="question.memberIds"></a-select>
        </template>
      </DynamicForm>
      <template #footer>
        <a-button key="back" @click="handleCancel">取消</a-button>
        <a-button v-if="currentLevel === 0 ? true : false" key="submit" type="primary" :loading="state.loading" @click="handleOk">{{ OK_TEXT[showType] }}</a-button>
      </template>
    </a-modal>
  </div>
</template>

<script lang="ts" setup>
import { computed, getCurrentInstance, onMounted, reactive, ref, watch } from 'vue';
import { DynamicForm } from '@/components/public-template';
import { RESETTABLE } from '@/mixins/subject/subject-types';
import viewService from './viewDialog.service';
import { message } from 'ant-design-vue';
import { projectGet, projectSave } from '@/config/api/projectFile';
import { changeByte } from '@/utils/common';
import { listByOrgId } from '@/config/api/deptFile';

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  data: {
    type: Object
  },
  // VIEW：查看 EDIT：编辑
  showType: {
    type: String,
    default: ''
  },
  isSecendLev: {
    type: Boolean,
    default: false
  },
  projectFile: {
    type: Array
  },
  currentLevel: {
    default: null
  }
});
const emit = defineEmits(['update:visible', 'handleEdit']);
const state = reactive({
  loading: false
});
const TITLE = {
  VIEW: '项目文件夹详情',
  EDIT: '编辑项目文件夹'
};
const OK_TEXT = {
  VIEW: '编辑',
  EDIT: '确定'
};
const directoryRef = ref(null);
const localState = {
  memberIdsOptions: []
};
const { proxy }: any = getCurrentInstance();
const dyConfig: any = {
  type: 'layer',
  grid: 12,
  labelCol: 12,
  wrapperCol: 24,
  layout: 'vertical',
  cancelSave: true,
  data: props.data,
  beforeWithMethod: (from) => {
    const data = JSON.parse(JSON.stringify(from));
    data.size = changeByte(data.size);
    data.type = data.folder ? '文件夹' : data.suffix && data.suffix.toLocaleUpperCase();
    return data;
  }
};
const dyQuestions: any = reactive(
  props.showType == 'VIEW' ? viewService.getViewQuestions(props.isSecendLev, props.data.projectFilePermissionResps, props.currentLevel) : viewService.getQuestions(props.isSecendLev)
);

onMounted(() => {
  // if (props.showType === 'EDIT') {
  //   props.data.projectFilePermissionResps.map((res) => {
  //     res.permission = Number(res.permission);
  //     res.memberIds = res.memberIds.split(',');
  //   });
  //   dyQuestions[8].value = [...props.data.projectFilePermissionResps];
  // }
  // getListByOrgId();
});

// const getListByOrgId = () => {
//   listByOrgId({ id: JSON.parse(window.localStorage.getItem('LOGIN__INFO__'))['orgId'] }).then((res) => {
//     if (res && res.success) {
//       localState.memberIdsOptions = res.data.map((item) => {
//         return { label: item.nickName, value: item.id, key: item.id };
//       });
//     }
//   });
// };

function handleOk() {
  if (props.showType == 'VIEW') {
    emit('handleEdit', props.data);
  } else {
    directoryRef.value.onFinish((params) => {
      state.loading = true;
      projectSave(params)
        .then((result) => {
          if (result.success) {
            message.success(`${props.showType == 'ADD' ? '添加' : '修改'}成功`);
            proxy.$NotificationFormSub.pushParams({ key: RESETTABLE, value: { name: 'projectFile' } });
            emit('update:visible', false);
          }
          state.loading = false;
        })
        .catch((err) => {
          state.loading = false;
          return Promise.reject(err);
        });
    });
  }
}
function handleCancel() {
  emit('update:visible', false);
}
</script>

<style lang="less" scoped>
:deep(.ant-table-cell .ant-row.ant-form-item) {
  margin-bottom: 0;
}
</style>
